<template>
	<view class="content">
		<view class="menu-block">
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">联系人</view>
				</view>
				<input v-model="form.contacts" placeholder="请输入联系人姓名" class="info-crl" maxlength="10"
					type="text" />
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">联系电话</view>
				</view>
				<input v-model="form.phone" placeholder="请输入联系电话" class="info-crl" maxlength="11"
					type="number" />
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">地区</view>
				</view>
				<view class="form-content">
					<input v-model="form.area" @click="chosenPosition" type="text"
						placeholder="请选择地区" disabled="true" placeholder-class="inputPlaceholder"
						class="form-ctrl" />
					<image src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/more.png" class="arrow"></image>
				</view>
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">详细地址</view>
				</view>
				<input v-model="form.address" placeholder="请输入详细地址" class="info-crl" type="text"
					maxlength="30" />
			</view>
			<view class="info-list">
				<view class="info-item">
					<view class="must">*</view>
					<view class="name">是否设为默认收货地址</view>
				</view>
				<view class="form-content">
					<picker class="form-ctrl" :range="work" range-key="value" :value="workIndex"
						@change="bindWork">
						{{workIndex==-1 ? "请选择是否设为默认收货地址" : work[workIndex]['value']}}
					</picker>
					<image src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/more.png" class="arrow"></image>
				</view>
			</view>
		</view>
		<view class="step" @click="handleSave">
			保存
		</view>
		
	</view>
</template>

<script>
	import {
		getAddressById,
		addAndUpdateAddress,
	} from '@/api/address.js';
	import {
		getCustomNavData,
		getCurrentTime,
		get24HoursTime,
		showToast
	} from '@/util/util.js'
	import {
		validate
	} from '@/util/validate.js'
	export default {
		data() {
			return {
				form: {
					contacts: '',
					phone: '',
					area:'',
					address:'',
					isDefault: 2,
				},
				type:1,
				rules: {
					contacts: {
						name: '联系人姓名',
						required: true
					},
					phone: {
						name: '联系人电话',
						required: true
					},
					area: {
						name: '实际用工所在地',
						required: true
					},
					address: {
						name: '详细地址',
						required: true
					},
					
				},
				id: null,
				work: [{
						id: 1,
						value: '是'
					},
					{
						id: 2,
						value: '否'
					}
				],
				workIndex: -1,
			}
		},
		methods: {
			bindWork(e) {
				this.workIndex = e.detail.value;
				this.form.isDefault = parseInt(e.detail.value) + 1;
				// this.form.isContract = this.work[this.workIndex].id
			},
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			//详情
			getAddressById() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getAddressById({
					id:this.id,
				}).then(res => {
					uni.hideLoading()
					this.form = res
					this.workIndex = this.form.isDefault - 1
				}).catch(() => {
					uni.hideLoading()
				})
			},
			chosenPosition() {
				//获取当前位置
				var that = this;
				const currentLocation = uni.getLocation({
					isHighAccuracy: true,
					success(current) {
						const currentLatitude = current.latitude;
						const currentLongitude = current.longitude;
						uni.chooseLocation({
							latitude: currentLatitude,
							longitude: currentLongitude,
							success: (choosen) => {
								delete choosen.errMsg;
								that.form.area = choosen.address;
							},
							fail: (err) => {
								showToast('选择位置失败，请重试！');
							}
						})
					},
					fail: (e) => {
						showToast('获取位置失败，请重试！');
					}
				})
			},
			handleSave() {
				let form = JSON.parse(JSON.stringify(this.form));
				form.contacts = this.form.contacts
				form.phone = this.form.phone
				form.area = this.form.area
				form.address = this.form.address
				form.isDefault = this.form.isDefault
				if (validate(form, this.rules)) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					addAndUpdateAddress(form).then((res) => {
						uni.hideLoading()
						// this.showToast(res, () => {
						// 	this.navigate('/page_other/submit/submit')
						// })
						uni.navigateBack()
					}).catch((res) => {
						uni.hideLoading()
						this.showToast('提交失败');
					})
				}
			}

		},
		onLoad(e) {
			if(e.id){
				this.id = e.id
				this.getAddressById()
			}
			//培训机构ID
		},
		onReachBottom() {
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fafafa;
	}
	.content {
		width: 100%;
		padding: 30rpx;
		.step{
			width: 70%;
			height: 100rpx;
			display: flex;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
			color: #fff;
			margin: 40rpx auto;
			border-radius: 60rpx;
			justify-content: center;
			align-items: center;
		}
		.menu-block {
			background: #fff;
			border-radius: 12px;
			padding: 30rpx;
			
			.info-list {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #F7F7F7;
				padding-bottom: 20rpx;
				margin-top: 20rpx;
			
				.img-item {
					display: flex;
					margin-top: 20rpx;
			
					.obverse-item {
						width: calc((100% - 30rpx) /2);
						margin-right: 30rpx;
			
						.upload-img {
							width: 100%;
							border-radius: 10rpx;
							height: 200rpx !important;
						}
					}
			
					.obverse-item:last-child {
						margin-right: 0;
					}
				}
			
				.info-item {
					display: flex;
					align-items: center;
			
					.must {
						color: #EA0000;
					}
			
					.formwork {
						color: #027BFF;
					}
				}
			
				.upload {
					display: flex;
					flex-direction: row;
					flex-wrap: wrap;
			
					.image-item {
						margin-top: 20rpx;
						margin-right: 20rpx;
						display: flex;
						flex-direction: row;
						// box-sizing: border-box;
						// border: 1px solid #efefef;
			
						.img {
							width: 190rpx;
							height: 200rpx;
							border-radius: 20rpx;
						}
			
						.iconfont {
							.delete {
								width: 20px;
								height: 20px;
								position: absolute;
								margin-left: -30rpx;
							}
						}
					}
			
					.image-item:nth-child(3n) {
						margin-right: 0;
					}
			
					.delete-item {
						width: 100%;
						display: flex;
						justify-content: flex-end;
			
						.delete {
							width: 20px;
							height: 20px;
							position: absolute;
						}
					}
			
					.image-item1 {
						display: flex;
						flex-direction: row;
			
						.delete {
							margin-top: 10rpx;
							margin-left: -40rpx;
							width: 20px;
							height: 20px;
							position: absolute;
						}
					}
			
					.upload-content {
						position: relative;
						background: #fafafa;
						border-radius: 5px;
						width: 200rpx;
						height: 200rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						margin-top: 20rpx;
			
						.title {
							position: absolute;
							top: 50%;
							left: 50%;
						}
			
						.upload {
							width: 90rpx;
							height: 90rpx;
						}
					}
				}
			
				.form-content {
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: space-between;
					margin-top: 20rpx;
			
					.check {
						transform: scale(0.7);
					}
			
					.form-ctrl {
						height: 80rpx;
						line-height: 80rpx;
						flex-grow: 1;
						color: #767676;
					}
			
					.arrow {
						flex-grow: 0;
						width: 40rpx;
						height: 40rpx;
					}
				}
			
				.radio-list {
					.radio-item {
						margin-top: 30rpx;
			
						.check {
							transform: scale(0.7);
						}
					}
				}
			
				.info-crl {
					margin-top: 30rpx;
				}
			}
		}
	}
</style>